package com.ddz.controller;

import com.ddz.model.LoginMessage;
import com.ddz.service.UserRegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class UserRegisterServlet extends HttpServlet {

    private UserRegisterService userRegisterService = new UserRegisterService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 接收客户发送的昵称、账号和密码
        String nickname = request.getParameter("nickname");
        String userId = request.getParameter("uid");
        String password = request.getParameter("upwd");
        String inviteCode = request.getParameter("inviteCode");

        // 调用userRegisterService判断玩家是否可以注册并返回相关信息
        LoginMessage loginMessage = userRegisterService.RegisterUser(nickname,userId,password,inviteCode);
        if(loginMessage.getStatus() == 200){
            request.setAttribute("regMessage", loginMessage);
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }else if(loginMessage.getStatus() == 100){
            request.setAttribute("regMessage", loginMessage);
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }

    }
}
